﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Service;


public partial class EquipmentTypes : System.Web.UI.Page
{

    readonly ReferenceDataService _ReferenceDataService = new ReferenceDataService();
    readonly StockService _StockService = new StockService();
    readonly  DirectService _DirectService = new DirectService();



    protected void Page_Load(object sender, EventArgs e)
    {
       // lblTotalTypeEquipment.Text = "Total equipment type:" + _DirectService.GetEquipmentTypeDisplayed(GetWhereClause(), GetOrderClause()).Rows.Count; 

        if (Session["whereClause"] == null)
            Session["whereClause"] = txtHiddenWhereClause.Text;
        if (Session["orderByClause"] == null)
            Session["orderByClause"] = txthiddenOrderbyClause.Text;

        new Utility().SetRowToDisplay(grdEquipmentType, Session["rowsToSDisplay"]);
        
        if (!IsPostBack)
        {
            // to avoid loading for the first time

            if (Session["pageIndex"] != null)
            {
                grdEquipmentType.PageIndex = Int32.Parse(Session["pageIndex"].ToString());

            }



            /* 
             * 1. number of equipments
             * 2. select the element 
             * */

            
        }
        else
        {
            //  SystemList = new DirectService().DataTableToIlistSystem(GetWhereClause(), GetOrderClause());
            txtHiddenPageIndex.Text = "0";
            lblTotalTypeEquipment.Text = "Total equipment type : " + new DirectService().GetEquipmentTypeDisplayed(GetWhereClause(), GetOrderClause()).Rows.Count;
            int number = +new DirectService().GetEquipmentTypeDisplayed(GetWhereClause(), GetOrderClause()).Rows.Count;
            if (number == 0)
            {
                grdEquipmentType1.Visible = true;
            }
            else
            {
                grdEquipmentType.Visible = true; 
                grdEquipmentType1.Visible = false;
            }
        }

        grdEquipmentType.PageIndex = !String.IsNullOrEmpty(txtHiddenPageIndex.Text) ? Int32.Parse(txtHiddenPageIndex.Text) : 0;
       
    }
    
    public int GetEquipmentTypeId()
    {
        return !String.IsNullOrEmpty(Request.QueryString["equipementTypeId"]) ? Int32.Parse(Request.QueryString["equipementTypeId"]) : 0;
    }

    public int GetPageIndex()
    {
        return Session["pageIndex"] != null
                   ?
                       Int32.Parse(Session["pageIndex"].ToString())
                   : 0;
    }
    public int GetTabIndex()
    {
        int tabIndex = Request.QueryString["tabIndex"] != null
                        ?
                            Int32.Parse(Request.QueryString["tabIndex"])
                        : 0;
        if (Session["defaultTabIndex"] != null)
        {
            tabIndex = Int32.Parse(Session["defaultTabIndex"].ToString());
            Session["defaultTabIndex"] = null;
        }

        return tabIndex;
    }

    protected string GetWhereClause()
    {
        string whereClause = string.Empty;
        string and = "";
        string str = "";

        // Prepare by equipement
        int i = 0;
        if (!string.IsNullOrEmpty(txtEquipmentTypeId.Text))
        {
            if (int.TryParse(txtEquipmentTypeId.Text, out i))
            {
                and = " and ";
                str = " where TypeEquipment.TypeEquipementId=" + Int32.Parse(txtEquipmentTypeId.Text);
                if (txtEquipmentTypeLabel.Text != string.Empty)
                {
                    str += " " + "and" + " " + "TypeEquipment.TypeEquipment=" + "'" + txtEquipmentTypeLabel.Text + "'";
                    and = " and ";
                }
            }else
            {
                grdEquipmentType.Visible = false;
                grdEquipmentType1.Visible = true;
                lblTotalTypeEquipment.Text = "Total equipment type : 0";
                return null;
            }
        }
        else
        {
            if (txtEquipmentTypeLabel.Text != string.Empty)
            {
                str += " " + "where" + " " + "TypeEquipment.TypeEquipment like" + "'%" + txtEquipmentTypeLabel.Text + "%'";
                and = " and ";
            }
        }

        if (!String.IsNullOrEmpty(str))
            whereClause = str + " ";
        return whereClause;
    }
    protected string GetOrderClause()
    {
        return " Order by TypeEquipment.TypeEquipment";
    }
    protected void bntFilter_Click(object sender, EventArgs e)
    {

        Session["whereClause"] = GetWhereClause();
        Session["orderByClause"] = GetOrderClause();
        txthiddenOrderbyClause.Text = GetOrderClause();
        txtHiddenWhereClause.Text = GetWhereClause();
        Session["defaultTabIndex"] = 0;
        ClearControlSearch();

    }
    private void ClearControlSearch()
    {

        txtEquipmentTypeId.Text = "";
        txtEquipmentTypeLabel.Text = "";

    }
    protected void grdEquipment_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

        grdEquipmentType.PageIndex = e.NewPageIndex;
        txtHiddenPageIndex.Text = grdEquipmentType.PageIndex.ToString();
        Session["pageIndex"] = grdEquipmentType.PageIndex;

    }
    protected void grdEquipment_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {

    }
    protected void grdSystem_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       
        if (e.CommandName.Equals("Delete"))
        {
           int  typeEquipmentId = Convert.ToInt32(grdEquipmentType.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
           
            _DirectService.DeleteEquipmentType(typeEquipmentId);
            Response.Redirect("EquipmentTypes.aspx");
            lblTotalTypeEquipment.Text = "Total equipment type:" + _DirectService.GetEquipmentTypeDisplayed(GetWhereClause(), GetOrderClause()).Rows.Count; 
        }
        new Utility().SetSelectedColorToGridView(e, grdEquipmentType);
        
    }
    protected void grdSystem_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void grdSystem_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void grdSystem_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {

    }
    protected void grdSystem_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

    }
    protected void grdEquipment_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void grdSystem_Sorted(object sender, EventArgs e)
    {

    }
    protected void grdEquipment_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //block to add message
            string stringComplete = "";
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                LinkButton deleteButton = (LinkButton)e.Row.Cells[1].Controls[0];
                deleteButton.Attributes.Add("onclick", "return confirm('" +Resources.Resource.ConfirmDeleteMessage+  "');");
            }
        }

    }
    protected void grdSystem1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void dsEquipmentType_Selected(object sender, ObjectDataSourceStatusEventArgs e)
    {
        if (e.ReturnValue != null) lblTotalTypeEquipment.Text = "Total equipment type:" + ((DataTable)e.ReturnValue).Rows.Count;
    }
}
